package org.soderiakelvy.controller.login;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.soderiakelvy.controller.util.Constante;
import org.soderiakelvy.controller.util.SelectOneMany;
import org.soderiakelvy.model.domain.DesProdTipoCliVO;
import org.soderiakelvy.model.domain.PackVO;
import org.soderiakelvy.model.domain.ProductoVO;
import org.soderiakelvy.model.domain.RolVO;
import org.soderiakelvy.model.service.LoginServicio;
import org.soderiakelvy.model.service.imp.LoginServicioImpl;


@ManagedBean
@ViewScoped
public class RolCreate implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private RolVO rol = new RolVO();
	private String deshabilitarGuardar = "true";
	private LoginServicio loginServicio = new LoginServicioImpl();
	
	public RolCreate() {
		System.out.println("Constructor rolCreate");
	}
	
	public String guardarRol(RolVO rol){
		
		rol.setNombre(rol.getNombre().trim().toUpperCase());
		
		if (loginServicio.insertRolVO(rol).equals("SUCCESS")){
			return Constante.URL_ROL_LIST;
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Error al insertar un Rol. ", "para continuar "));
			return null;
		}
	}
	
	public String cancelar(){
		System.out.println("cancelar");
		return Constante.URL_ROL_LIST;
	}
	
	//---------------------VALIDACION------------------------------------------------
	public void listenerNombre(){
		if (this.rol.getNombre().isEmpty()||this.rol.getNombre() ==null){
			this.deshabilitarGuardar = "true";
			FacesContext.getCurrentInstance().addMessage("form1::errorNombre", 
					new FacesMessage(FacesMessage.SEVERITY_FATAL,"El Nombre del Rol no puede ser Vacio", "para continuar "));
		}else{
			if (loginServicio.existeNombreRol(this.rol.getNombre().toUpperCase())){
				FacesContext.getCurrentInstance().addMessage("form1::errorNombre", 
						new FacesMessage(FacesMessage.SEVERITY_FATAL,"El Nombre del Rol ya Existe", "para continuar "));
				this.deshabilitarGuardar = "true";  
				
			}else{ 
				this.deshabilitarGuardar = "false"; //HABILITO GUARDAR
			}
		}
	}
	
	//-------------------- GETTERES AND SETTERS--------------------------------------
	public RolVO getRol() {
		return rol;
	}

	public void setRol(RolVO rol) {
		this.rol = rol;
	}

	public String getDeshabilitarGuardar() {
		return deshabilitarGuardar;
	}

	public void setDeshabilitarGuardar(String deshabilitarGuardar) {
		this.deshabilitarGuardar = deshabilitarGuardar;
	}
	

}
